<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/31
 * Time: 14:57
 */

namespace app\portal\controller;
use cmf\controller\AdminBaseController;
use think\Db;


class AdminControllController extends AdminBaseController
{
    public function index(){
        $data=Db::name('traffic_controll')->paginate(20);
        $this->assign('data',$data);
        $this->assign('page',$data->render());
        return $this->fetch();
    }
    //添加交管员
    public function controll_add(){
        $id=input('id');
        if($id){
            $data=Db::name('traffic_controll')->where('id',$id)->find($id);
            $this->assign('data',$data);
        }
        return $this->fetch();
    }
    //保存添加修改社区交管员数据
    public function controll_save(){
        $data=input('post.');
        unset($data['file']);
        if(empty($data['id'])){
            unset($data['id']);
            $result = Db::name('traffic_controll')->insert($data);
            if ($result === false) {
                $this->error('添加失败!');
            }
            $this->success('添加成功!', url('AdminControll/index'));
        }else{
            $result = Db::name('traffic_controll')->update($data);
            if ($result === false) {
                $this->error('修改失败!');
            }
            $this->success('修改成功!', url('AdminControll/index'));
        }
    }
    //直接修改
    public function save_change(){
        $id=input('id');
        $patrol=input('patrol');
        $name=input('name');
        $result=[];
        $res=Db::name('traffic_controll')->where('id',$id)->update([$name=>$patrol]);
        $result['code']=$res;
        if($res){
           $result['msg']= '修改成功！';
        }else{
            $result['msg']=  '修改失败';
        }
        return json($result);
    }
    //添加修改
    public function save_add(){
        $id=input('id');
        $name=input('name');
        $code=input('code');
        $result=[];
        try{
            Db::name('traffic_controll')->where('id',$id)->setInc($name,$code);
            $result['error']=false;
            $result['errmsg']='修改成功';
        }catch (\Exception $e){
            $result['error']=true;
            $result['errmsg']=$e->getMessage();
        }
        return json($result);
    }

    public function talent_add(){
        $id=input('id');
        $data=['status'=>1];
        $res=Db::name('traffic_controll')->where('id',$id)->update($data);
        if ($res === false) {
            $this->error('添加失败!');
        }
        $this->success('添加成功!', url('AdminControll/index'));
    }
    public function talent_cancel(){
        $id=input('id');
        $data=['status'=>0];
        $res=Db::name('traffic_controll')->where('id',$id)->update($data);
        if ($res === false) {
            $this->error('撤销失败!');
        }
        $this->success('撤销成功!', url('AdminControll/index'));
    }
    public function del(){

        $id= $this->request->param('id');
        $result=Db::name('traffic_controll')->where('id',$id)->delete();
        if ($result) {
            $this->success('删除成功!');
        } else {
            $this->error('删除失败');
        }
    }

}